package com.hyny.framework.platform.system.utils.bean;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyny.framework.platform.system.utils.enums.ResultFieldPowerEnum;
import com.hyny.framework.platform.system.utils.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 聚合平台统一响应结果
 */
@ApiModel("公共响应参数")
@Data
@AllArgsConstructor
@NoArgsConstructor
public final class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value="状态码" ,position=1)
    private String code;
    @ApiModelProperty(value="描述" ,position=2)
    private String message;
    @ApiModelProperty(value="响应信息" ,position=3)
    private T data;

    @ApiModelProperty(value="字段权限标识" ,position=4)
    private ResultFieldPowerEnum resultFieldPowerEnum;


    public Result(String code, String message, T data){
        this.code=code;
        this.message=message;
        this.data=data;
    }


    public static Result ok() {
        return new Result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),
                null);
    }

    public static Result ok(ResultEnum resultEnum) {
        return new Result(resultEnum.getCode(), resultEnum.getMsg(), null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),
                data);
    }

    public static <T> Result<T> ok(String message) {
        return new Result(ResultEnum.SUCCESS.getCode(), message,"");
    }
    public static <T> Result<T> ok(String message, T data) {
        return new Result(ResultEnum.SUCCESS.getCode(), message, data);
    }


    public static <T> Result<T> okObject(T data) {
        return okObject(ResultEnum.SUCCESS,data);
    }

    public static <T> Result<T> okObject(ResultEnum resultEnum, T data) {

        return new Result(resultEnum.getCode(), resultEnum.getMsg(), data,ResultFieldPowerEnum.R_OBJECT);
    }


    public static <T> Result<T> okPage(IPage data) {
        return okPage(ResultEnum.SUCCESS,data);
    }

    public static <T> Result<T> okPage(ResultEnum resultEnum,IPage data) {
        return new Result(resultEnum.getCode(), resultEnum.getMsg(), data,ResultFieldPowerEnum.R_PAGE);
    }

    public static <T> Result<T> okList(List<T> data) {
        return  okList(ResultEnum.SUCCESS,data);
    }
    public static <T> Result<T> okList(ResultEnum resultEnum,List<T> data) {

        return new Result(resultEnum.getCode(), resultEnum.getMsg(), data,ResultFieldPowerEnum.R_LIST);
    }



    public static Result error(String message) {
        return new Result(ResultEnum.ERROR.getCode(), message, null);
    }

    public static Result error(ResultEnum resultEnum) {
        return new Result(resultEnum.getCode(), resultEnum.getMsg(), null);
    }

    public static Result error(ResultEnum resultEnum, String message) {
        return new Result(resultEnum.getCode(), resultEnum.getMsg()+":"+message, null);
    }

    public static Result error(ResultEnum resultEnum, String message, String data) {
        return new Result(resultEnum.getCode(), resultEnum.getMsg()+":"+message, data);
    }
}
